package Tree;

import java.util.ArrayList;
import java.util.List;

public class _129_SumRoottoLeafNumbers {
    List<String> res = new ArrayList<>();
    public int sumNumbers(TreeNode root) {
        if(root==null) return 0;
        DFS(root,"");
        int sum = 0;
        for (String s : res) {
            sum += Integer.parseInt(s);
        }
        return sum;
    }

    public void DFS(TreeNode root, String val) {
        if (root != null) {
            if (root.left == null && root.right == null) {
                res.add(val+root.val);
            } else {
                DFS(root.left, val + root.val);
                DFS(root.right, val + root.val);
            }
        }
    }
}
